package com.boilerCloud.dao.order;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.DeviceInfo;
import com.boilerCloud.dto.order.SalesOrder;
import com.boilerCloud.util.StringUtil;

@Repository
public class OrderDao extends BaseDao {

	public void qryList(SalesOrder order, HzlqswPage<SalesOrder> page)throws Exception {
		// TODO Auto-generated method stub
		String countsql = "select count(*) from sales_order a "
				+ " left join app_user b on b.id = a.dealer_id"
				+ " left join app_user d on d.id = a.user_id"
				+ " left join dictionary c on c.id = a.device_type"
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.*,c.name typeName,b.name dealerName,d.name userName "
					+ " from sales_order a "
					+ " left join app_user b on b.id = a.dealer_id"
					+ " left join app_user d on d.id = a.user_id"
					+ " left join dictionary c on c.id = a.device_type"
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(order.getOrderNo())){
			countsql += " and a.order_no like ?";
			listSql += " and a.order_no like ?";
			params.add("%" + order.getOrderNo() +"%");
		}
		if(order.getStatus() == 1 || order.getStatus() == 8){
			countsql += " and a.status = ?";
			listSql += " and a.status = ?";
			params.add(order.getStatus());
		}else{
			countsql += " and a.status > 1   and a.status < 8";
			listSql += " and a.status > 1   and a.status < 8";
		}
		
		if(!StringUtil.isEmpty(order.getDealerName())){
			countsql += " and b.name like ?";
			listSql += " and b.name like ?";
			params.add("%" + order.getDealerName() +"%");
		}
		if(!StringUtil.isEmpty(order.getUserName())){
			countsql += " and d.name like ?";
			listSql += " and d.name like ?";
			params.add("%" + order.getUserName() +"%");
		}
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, SalesOrder.class);
		
	}

	public void handleSalesOrder(SalesOrder order) throws Exception{
		String sql = "update sales_order set status=? where id =?";
		this.jdbcTemplate.update(sql,order.getStatus(),order.getId());
		
	}

	public void addSalesOrder(SalesOrder device)throws Exception {
		String sql = "insert into sales_order(order_no,device_no,device_name,require_time,"
				+ "dealer_id,user_id,device_type,status,create_time,modify_time,is_delete)"
				+ " values(?,?,?,?,?,?,?,1,now(),now(),'n')";
		List<Object> param = new ArrayList<Object>();
		param.add(device.getOrderNo());
		param.add(device.getDeviceNo());
		param.add(device.getDeviceName());
		param.add(device.getRequireTimeStr());
		param.add(device.getDealerId());
		param.add(device.getUserId());
		param.add(device.getDeviceType());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}

	public SalesOrder getOrderByOrderNo(String orderNo) throws Exception {
		String sql = "select * from sales_order where order_no= ? limit 1";
		return this.queryForObject(sql, SalesOrder.class,orderNo);
		
	}

	public void addOrderRecord(int id, String statename, int state) {
		String sql = "insert into order_Record(order_id,state,create_time,state_name)"
				+ " values(?,?,now(),?)";
		this.jdbcTemplate.update(sql,id,state,statename);
		
	}

}
